package cn.edu.hzvtc.controller;

import cn.edu.hzvtc.pojo.Area;
import cn.edu.hzvtc.pojo.ReturnMsg;
import cn.edu.hzvtc.service.areaService;
import cn.edu.hzvtc.service.userService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/area")
public class AreaController {
    @Autowired
    private areaService areaService;
    @Autowired
    private userService userService;
    @RequestMapping(value = "/tree")
    @ResponseBody
    public ReturnMsg getTreeList(){
        List<Area> areas =areaService.getTree(0);
        return ReturnMsg.success().add("areas",areas);

    }


    @RequestMapping(value = "/school", method = RequestMethod.POST)
    @ResponseBody
    public ReturnMsg saveArea(@Valid Area area, BindingResult bindingResult) {
        //数据绑定出错
        if (bindingResult.hasErrors()) {
            Map<String, Object> map = new HashMap<String, Object>();
            for (FieldError fieldError : bindingResult.getFieldErrors()) {
                map.put(fieldError.getField(), fieldError.getDefaultMessage());
            }
            return ReturnMsg.fail().add("fieldErrors", map);
        } else {
            System.out.println(areaService.add(area));
                return ReturnMsg.success();
        }
    }

    @RequestMapping(value = "/school/{edit-id}", method = RequestMethod.PUT)
    @ResponseBody
    public ReturnMsg update(@PathVariable("edit-id")Integer areaId, @Valid Area area, BindingResult result) {
        //修改操作
        area.setId(areaId);
        areaService.update(area);
        return ReturnMsg.success();
    }

    @RequestMapping(value = "/school/{delete-id}", method = RequestMethod.DELETE)
    @ResponseBody
    public ReturnMsg delete(@PathVariable("delete-id")Integer areaId, @Valid Area area, BindingResult result) {
        //删除操作
        areaService.delete(areaId);
        return ReturnMsg.success();
    }

    @RequestMapping("/list")
    @ResponseBody
    public ReturnMsg getAreaList(@RequestParam(value = "parentId", defaultValue = "0") Integer parentId) {
        List<Area> areaList=areaService.getChildren(parentId);
        return ReturnMsg.success().add("areaList",areaList);
    }

    //首页显示功能
    @RequestMapping("/count")
    @ResponseBody
    public ReturnMsg getCount() {
        Long provinceCount = areaService.getCount(1);
        Long cityCount = areaService.getCount(2);
        Long schoolCount = areaService.getCount(3);
        Long userCount= userService.getCount(0);

        return ReturnMsg.success().add("provinceCount",provinceCount)
                .add("cityCount",cityCount)
                .add("schoolCount",schoolCount)
                .add("userCount",userCount);
    }
}
